package swd.maintain.project.prg_in_sys_maintain;

import com.pub.database.*;

public class Business {
    QueryBean gqbnDAO = null;
    public Business(QueryBean pqbnDAO) {
        gqbnDAO = pqbnDAO;
    }

    String exeInsSQL(View view) throws Exception {
        String lstrSQL = new String();
        try {
         String temp="";
         String   joinsys=gqbnDAO.readSingleValueData("select join_sys||'' from wkf_prg  where prg_no='"+view.getprg_no()+"'");
         if (joinsys!=null && !joinsys.equals("") ){
              if (joinsys.indexOf(view.getins_sys_no().trim())>=1){
                  temp=joinsys;
                  return "系統編號已存在";
              }else{
                  temp =joinsys+","+view.getins_sys_no().trim();
              }

         }else{
              temp =view.getins_sys_no().trim();
         }

         lstrSQL="update wkf_prg set join_sys='"+temp+"' where prg_no='"+view.getprg_no()+"'";
         gqbnDAO.executeSQL(lstrSQL);

            return "";
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }


    void exeDelSQL(View view, int pintSn) {
        String lstrSQL = new String();
        try {
            String temp="";
            String   joinsys[]=gqbnDAO.readSingleValueData("select join_sys from wkf_prg  where prg_no='"+view.getprg_no()+"'").split(",");
            if (joinsys!=null ){

                for (int i=0;i<joinsys.length;i++){
                    if (!view.getsprg_no()[pintSn].trim().equals(joinsys[i])){
                        if  (i==(joinsys.length-1)){
                            temp += joinsys[i];
                        }else{
                            temp += joinsys[i]+",";
                        }
                    }
                }
            }

            lstrSQL="update wkf_prg set join_sys='"+temp+"' where prg_no='"+view.getprg_no()+"'";
            /*
            lstrSQL = "delete from wkf_prg where prg_no='" +
                      view.getsprg_no()[pintSn].trim() + "' ";
            gqbnDAO.executeSQL(lstrSQL);

            lstrSQL = "delete from wkf_sys_prg  where prg_no='" +
                      view.getsprg_no()[pintSn].trim() + "' and sys_no='" +
                      view.gets_no()[pintSn].trim() + "' ";*/
            gqbnDAO.executeSQL(lstrSQL);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    void setDataViewForm(DataViewForm DataViewForm, View view) throws Exception {

        //StringBuffer stbCondition = new StringBuffer();
        StringBuffer stbPageActionURI = new StringBuffer(
                "prg_in_sys_maintain.jsp?rowsn=");
        String strWhereSQL = "";
        /*
        DataViewForm.setFieldsList(
                " '' ,p.prg_no,p.prg_name,p.dbf_no,p.join_sys");
        DataViewForm.setTablesList(" wkf_prg as p left outer join wkf_sys_prg as sys on p.prg_no=sys.prg_no ");
        strWhereSQL = " 1=1";
        if (view.getprg_no() != null && !view.getprg_no().trim().equals("")) {
            strWhereSQL += " and p.prg_no like '" + view.getprg_no().trim() +
                    "%' ";

        }
        if (view.getprg_cname() != null &&
            !view.getprg_cname().trim().equals("")) {
            strWhereSQL += " and p.prg_name like '" + view.getprg_cname().trim() +
                    "%'   ";

        }*/
      String joinsys="";
        DataViewForm.setFieldsList(" '', sys_no,sys_name,sys_manager,active ");
        DataViewForm.setTablesList("wkf_sys");
        if (view.getprg_no()!=null ){
            joinsys=gqbnDAO.readSingleValueData("select join_sys from wkf_prg  where prg_no='"+view.getprg_no()+"'");
            joinsys=joinsys!=null ?joinsys.replace(",","','"):"";
            strWhereSQL =" sys_no in  ('"+joinsys+"')";
        }else{
            strWhereSQL=" sys_no in ('')";
        }


        DataViewForm.setSQLCondition(strWhereSQL);
        DataViewForm.setGroupBy(null);
        DataViewForm.setHaving(null);

        String strColRelation[][] = new String[5][];

        strColRelation[0] = new String[5];
        strColRelation[1] = new String[5];
        strColRelation[2] = new String[5];
        strColRelation[3] = new String[5];
        strColRelation[4] = new String[5];

        strColRelation[0][0] = "''";
        strColRelation[0][1] = "INPUT";
        strColRelation[0][2] = "WIDTH='10%' align='center'";
        strColRelation[0][3] = "<INPUT class='button b_normal' TYPE=\"BUTTON\" onclick=\"javascript:saveRowSn('%sn%');document.all.item('prgAction').value='delete';document.forms[0].submit();\" NAME=\"del\" VALUE=";
        strColRelation[0][4] = "'刪除'";

        strColRelation[1][0] = "'系統編號'";
        strColRelation[1][1] = "INPUT";
        strColRelation[1][2] = "WIDTH='20%'";
        strColRelation[1][3] = "<INPUT TYPE='text' size='8' name='sprg_no' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[1][4] = "1";

        strColRelation[2][0] = "'系統編號'";
        strColRelation[2][1] = "INPUT";
        strColRelation[2][2] = "WIDTH='30%'";
        strColRelation[2][3] = "<INPUT TYPE='text' size='40' name='sprg_name' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[2][4] = "2";

        strColRelation[3][0] = "'維護人員工號'";
        strColRelation[3][1] = "INPUT";
        strColRelation[3][2] = "WIDTH='10%'";
        strColRelation[3][3] = "<INPUT TYPE='text' size='15' name='sdbf_no' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[3][4] = "3";

        strColRelation[4][0] = "'使用中(Y/N)'";
        strColRelation[4][1] = "INPUT";
        strColRelation[4][2] = "WIDTH='40%'";
        strColRelation[4][3] = "<INPUT TYPE='text' size='30' name='s_no' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[4][4] = "4";

        DataViewForm.setColRelation(strColRelation); //將欄位相關設定殖入ActionForm
        DataViewForm.setJumpPageButtonShow(true); //是否要秀出上下頁按鈕
        DataViewForm.setPageBtnOnTop(false); //將button and rows info 秀在頂端
        DataViewForm.setPageBtnOnButtom(true); //將button and rows info 秀在底端
        DataViewForm.setFilterShow(false); //是否要秀出過濾選擇欄位
        DataViewForm.setJumpPageShow(true); //是否要秀出跳頁下拉式選單
        DataViewForm.setRowsInfoShow(false); //是否要秀出筆數資訊
        DataViewForm.setRowPageShow(false); //是否要秀出每頁筆數控制欄
        DataViewForm.setExcelShow(false); //是否要秀出轉Excel button
        DataViewForm.setOrderEnable(false); //是否要提供排序點選功能
        DataViewForm.setShowHeaderWhenNoData(true); //當ResultSet無資料是否要秀Header

        DataViewForm.setTableHeaderHtml(
                "class='td_head_grid' align='center'  width='680px'");
        //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)
        DataViewForm.setPageActionURI(stbPageActionURI.toString());
    }



    String checkNotNullFld(View view) throws Exception {
        StringBuffer lstbNullFldList = new StringBuffer();
        lstbNullFldList.append(view.getins_sys_no() == null ?
                               "系統代碼不得為空值!" + "," : "");

        return (lstbNullFldList.toString().equals("") ? "" :
                lstbNullFldList.
                delete(lstbNullFldList.length() - 1, lstbNullFldList.length()).
                toString());
    }

    void setInitValueForAdd(View view, String pstrUserID) throws Exception {
    }

    void setReadonlyForAdd(View view) throws Exception {

    }

}
